<template>
  <div class="login">
    <el-form :model="user" :rules="rules" ref="userForm" label-width="80px" class="demo-ruleForm">
      <el-form-item label="账号" prop="userName">
        <el-input v-model="user.userName"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input v-model="user.password" type="password" autocomplete="off" @keyup.enter.native="loginSubmit('userForm')"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="loginSubmit('userForm')" style="margin-left: 37%;">登陆</el-button>
        <router-link :to="'/dm/register'" class="link">注册</router-link>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>

  import {userService} from '../../service/userService'

  export default {
    name: 'login',
    data () {
      return {
        user: {
          userName: '',
          password: ''
        },
        rules: {
          userName: [{required: true, message: '请输入账号', trigger: 'blur'}],
          password: [{required: true, message: '请输入密码', trigger: 'blur'},
            {min: 6, max: 8, message: '长度在6到8个字符', trigger: 'blur'}]
        }
      }
    },
    created () {
      console.log(this.$store.getters.userInfo);
    },
    methods: {
      loginSubmit(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            this.$store.dispatch('loginSubmit', this.user).then(() => {
              this.$router.push({path: '/'})
            }).catch(() => {
              console.log('error submit!!');
            })
          } else {
            console.log('error submit!!');
            return false;
          }
        })
      },
    },
    register () {
      this.$router.push('/dm/register')
    }
  }
</script>

<style lang="scss" scoped>
  .link {
    color: #409EFF;
    text-decoration: none;
    float: right;
    padding-top: 3%;
  }
</style>
